#include "storage_subsystem.h"

void deal_user(char *buf,char *addr){
    //解析
    cJSON *json_buf=NULL;
    json_buf= cJSON_Parse(buf);
    cJSON *token_json=NULL;
    token_json= cJSON_GetObjectItem(json_buf,"token");
    cJSON *name_json=NULL;
    cJSON *password_json=NULL;
    name_json= cJSON_GetObjectItem(token_json,"name");
    password_json= cJSON_GetObjectItem(token_json,"password");
    //封装
    cJSON *json_new=NULL;
    json_new=cJSON_CreateObject();
    cJSON_AddStringToObject(json_new,"边缘计算子系统IP地址",addr);
    char *data_user=NULL;
    data_user= cJSON_Print(json_new);
    std::cout<<data_user<<std::endl;
    //释放空间
    cJSON_Delete(json_buf);
    cJSON_Delete(json_new);
    //保存用户信息
    std::ofstream fout;
    fout.open(R"(/home/kylin/data/store/users.dat)",std::ios_base::out | std::ios_base::app );
    fout<<data_user<<std::endl;
    fout.close();
}

void deal_data(char *buf){
    //解析
    cJSON *json_buf=NULL;
    json_buf= cJSON_Parse(buf);
    cJSON *cjson_time=NULL;
    cJSON *cjson_temperature=NULL;
    cJSON *cjson_humidity=NULL;
    cJSON *cjson_speed=NULL;
    cJSON *cjson_direction=NULL;
    cJSON *cjson_rain=NULL;
    cJSON *cjson_sunlight=NULL;
    cJSON *cjson_pm25=NULL;
    cJSON *cjson_pm10=NULL;
    cJSON *cjson_station=NULL;
    cjson_station= cJSON_GetObjectItem(json_buf,"station");
    cjson_direction= cJSON_GetObjectItem(json_buf,"direction");
    cjson_humidity= cJSON_GetObjectItem(json_buf,"humidity");
    cjson_pm10= cJSON_GetObjectItem(json_buf,"pm10");
    cjson_pm25= cJSON_GetObjectItem(json_buf,"pm25");
    cjson_time= cJSON_GetObjectItem(json_buf,"time");
    cjson_temperature= cJSON_GetObjectItem(json_buf,"temperature");
    cjson_speed= cJSON_GetObjectItem(json_buf,"speed");
    cjson_rain= cJSON_GetObjectItem(json_buf,"rain");
    cjson_sunlight= cJSON_GetObjectItem(json_buf,"sunlight");
    //封装
    cJSON *json_data=NULL;
    json_data=cJSON_CreateObject();
    //cJSON_AddNumberToObject(json_data,"采集日期时间",cjson_time->valuedouble);

    time_t ttttt=cjson_time->valuedouble;
    tm *ltm=localtime(&ttttt);
    std::cout<<ltm->tm_hour<<":"<<ltm->tm_min<<":"<<ltm->tm_sec<<std::endl;
    string s_t="";
//    Convert(ltm->tm_hour);
//    s_t+=":";
//    s_t+= Convert(ltm->tm_min);
//    s_t+=":";
//    s_t+= Convert(ltm->tm_sec);
    if(ltm->tm_hour<10){
        s_t+="0";
        s_t+=Convert(ltm->tm_hour);
    }else{
        s_t+=Convert(ltm->tm_hour);
    }
    s_t+=":";
    if(ltm->tm_min<10){
        s_t+="0";
        s_t+=Convert(ltm->tm_min);
    }else{
        s_t+=Convert(ltm->tm_min);
    }
    s_t+=":";
    if(ltm->tm_sec<10){
        s_t+="0";
        s_t+=Convert(ltm->tm_sec);
    }else{
        s_t+=Convert(ltm->tm_sec);
    }

    cJSON_AddStringToObject(json_data,"采集日期时间",s_t.c_str());

    cJSON_AddNumberToObject(json_data,"温度",cjson_temperature->valuedouble);
    cJSON_AddNumberToObject(json_data,"湿度",cjson_humidity->valuedouble);
    cJSON_AddNumberToObject(json_data,"风速",cjson_speed->valuedouble);
    cJSON_AddNumberToObject(json_data,"风向",cjson_direction->valuedouble);
    cJSON_AddNumberToObject(json_data,"降雨量",cjson_rain->valuedouble);
    cJSON_AddNumberToObject(json_data,"光照",cjson_sunlight->valuedouble);
    cJSON_AddNumberToObject(json_data,"PM2.5",cjson_pm25->valuedouble);
    cJSON_AddNumberToObject(json_data,"PM10",cjson_pm10->valuedouble);


    char *data= cJSON_Print(json_data);
    //释放空间
    cJSON_Delete(json_data);
//    string data="[";
//    data.append(Convert(cjson_time->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_temperature->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_humidity->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_speed->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_direction->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_rain->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_sunlight->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_pm25->valuedouble));
//    data.append(",");
//    data.append(Convert(cjson_pm10->valuedouble));
//    data.append("]\n");
//    time_t ttttt=cjson_time->valuedouble;
//    tm *ltm=localtime(&ttttt);
//    auto s_t= Convert(ltm->tm_hour);
//    s_t+=":";
//    s_t+= Convert(ltm->tm_min);
//    s_t+=":";
//    s_t+= Convert(ltm->tm_sec);
//    string data[9];
//    data[0]=s_t;
//    cout<<s_t<<endl;
//    data[1]= Convert(cjson_temperature->valuedouble);
//    data[2]= Convert(cjson_humidity->valuedouble);
//    data[3]= Convert(cjson_speed->valuedouble);
//    data[4]= Convert(cjson_direction->valuedouble);
//    data[5]= Convert(cjson_rain->valuedouble);
//    data[6]= Convert(cjson_sunlight->valuedouble);
//    data[7]= Convert(cjson_pm25->valuedouble);
//    data[8]= Convert(cjson_pm10->valuedouble);
//    cout<<data;
    cJSON_Delete(json_buf);
    //保存气象数据
    std::ofstream fout;
    //用时间自动刷新文件
    auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    //转为字符串
    std::stringstream ss;
    // 可以分别以不同的形式进行显示
    //ss << std::put_time(std::localtime(&t), "%Y-%m-%d-%H-%M-%S");
    //ss << std::put_time(std::localtime(&t), "%Y年%m月%d日%H时%M分%S秒");
    //ss << std::put_time(std::localtime(&t), "%Y%m%d%H%M%S");
    ss<<std::put_time(std::localtime(&t),"%Y-%m-%d");
    std::string str_time = ss.str();
    if(!is_have_date(str_time,"/home/kylin/data/store/mydate.txt")){
        ofstream fout_date;
        fout_date.open("/home/kylin/data/store/mydate.txt",ios_base::out|ios_base::app);
        fout_date<<str_time<<endl;
        fout_date.close();
    }

    string path_data = "/home/kylin/data/store/station";
    if (cjson_station->valueint==1){
         path_data=path_data+"1/"+str_time;
    }else if(cjson_station->valueint==2){
        path_data=path_data+"2/"+str_time;
    }
    cout<<"气象数据的存储路径："<<path_data<<endl;
    fout.open(path_data,std::ios_base::out | std::ios_base::app | std::ios_base::binary);
    fout<<data<<std::endl;
    fout.close();
}


void communication_to_user(){

    int which_station=-1;

    int server_sockfd = 0;
    int len = 0;
    socklen_t sin_size = 0;
    char buf[1024*64];
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(9092);

    if((server_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!(storage subsystem)\n");
        return;
    }

    //
    int send_buffer_size=65535;
    setsockopt(server_sockfd,SOL_SOCKET,SO_SNDBUF,&send_buffer_size,sizeof(send_buffer_size));

    if(bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        perror("bind error!(storage subsystem)\n");
        return ;
    }
    //记录密码是否正确
    int success=0;
    int success_=0;
    while(1){
        sin_size= sizeof(struct sockaddr_in);
        if((len = recvfrom(server_sockfd, buf, 1024*64, 0, (struct sockaddr*)&client_addr, &sin_size)) < 0)
        {
            perror("recvfrom error!(storage subsystem)\n");
        }

        printf("recv from: %s\n",inet_ntoa(client_addr.sin_addr));
        buf[len] = '\0';
        printf("recv is: %s\n",buf);
        if(len <= 0)
        {
            perror("recvfrom() no read something\n");
        }
        cJSON *mjson=NULL;
        cJSON *code_json=NULL;

        mjson= cJSON_Parse(buf);

        code_json= cJSON_GetObjectItem(mjson,"code");
        int code_one;
        code_one=code_json->valueint;
        if(code_one==1){
            cout<<"第一个code"<<endl;
            cout<<buf<<endl;
            cJSON *name_json=NULL;
            cJSON *password_json=NULL;
            name_json= cJSON_GetObjectItem(mjson,"name");
            password_json= cJSON_GetObjectItem(mjson,"password");
            //此处应该检查账号密码

            if(strcmp(password_json->valuestring,"123456")!=0&&strcmp(name_json->valuestring,"root")!=0){
                //perror("密码错误");
                cJSON* cjson_token = NULL;
                cjson_token= cJSON_CreateObject();
                cJSON_AddNumberToObject(cjson_token, "code", 2);
                cJSON_AddNumberToObject(cjson_token, "success", 1);
                Token token_to_user(16);
                cJSON_AddStringToObject(cjson_token, "token", token_to_user.getToken().c_str());
                char *buf_token=NULL;
                buf_token= cJSON_Print(cjson_token);
//                if(sendto(server_sockfd, buf_token, strlen(buf_token), 0 ,(struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
//                    perror("sendto error(communication_to_user3.1)\n");
//                    cJSON* cjson_token = NULL;
//                    cjson_token= cJSON_CreateObject();
//                    cJSON_AddNumberToObject(cjson_token, "code", 2);
//                    cJSON_AddNumberToObject(cjson_token, "success", 1);
//                    cJSON_AddStringToObject(cjson_token, "token", "asdfghjklzxcvbnm");
//                    char *buf_token=NULL;
//                    buf_token= cJSON_Print(cjson_token);
                if(sendto(server_sockfd, buf_token, strlen(buf_token), 0 ,(struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
                    perror("sendto error(communication_to_user(code2)\n");
                }

            }
            else{
                success = 1;
                success_=1;
                //生成应答
                //3.1
                cJSON *cjson_token = NULL;
                cjson_token = cJSON_CreateObject();
                cJSON_AddNumberToObject(cjson_token, "code", 2);
                cJSON_AddNumberToObject(cjson_token, "success", 1);
                //应该做一些事情生成令牌......
                Token token_to_user(16);
                cJSON_AddStringToObject(cjson_token, "token", token_to_user.getToken().c_str());
                char *buf_token = NULL;
                buf_token = cJSON_Print(cjson_token);
                if (sendto(server_sockfd, buf_token, strlen(buf_token), 0, (struct sockaddr *) &client_addr,
                           sizeof(struct sockaddr)) < 0) {
                    perror("sendto error(communication_to_user(code2))\n");
                }
                cout << buf_token << endl;
            }
        }else if(code_one==3){
            cout<<"第3个code"<<endl;
            cout<<buf<<endl;
            //3.2
            //需要做些事件作废令牌
            cJSON* t_cjson= cJSON_GetObjectItem(mjson,"token");

            //下面这里是作废成功
            if(Token::removeToken(t_cjson->valuestring)){
                cJSON *cjson_token = NULL;
                cjson_token = cJSON_CreateObject();
                cJSON_AddNumberToObject(cjson_token, "code", 4);
                cJSON_AddStringToObject(cjson_token, "token", t_cjson->valuestring);
                cJSON_AddNumberToObject(cjson_token, "success", 1);
                char *buf_token = NULL;
                buf_token = cJSON_Print(cjson_token);
                if (sendto(server_sockfd, buf_token, strlen(buf_token), 0, (struct sockaddr *) &client_addr,
                           sizeof(struct sockaddr)) < 0) {
                    perror("sendto error(communication_to_user(code4))\n");
                }
                cout << buf_token << endl;
                //break;
            }
        }else if(code_one==5){
            cout<<"第5个code"<<endl;
            cout<<buf<<endl;

            cJSON* t_cjson= cJSON_GetObjectItem(mjson,"token");
            if (Token::findToken(t_cjson->valuestring) != t_cjson->valuestring){
                continue;
            }
            //3.3
            cJSON* cjson_token=NULL;
            cjson_token= cJSON_CreateObject();
            cJSON_AddNumberToObject(cjson_token, "code", 6);
            cJSON *cjson_data=NULL;
            cJSON *cjson_array=NULL;
            cjson_array=cJSON_CreateArray();
            cjson_data=cJSON_CreateObject();
            //cJSON_AddNumberToObject(cjson_data,"station",1);
            cJSON_AddStringToObject(cjson_data,"station","1");
            cJSON_AddStringToObject(cjson_data,"name","station1");
            cJSON_AddStringToObject(cjson_data,"station","2");
            cJSON_AddStringToObject(cjson_data,"name","station2");
            //cJSON_AddItemToArray(cjson_array, cJSON_CreateString(cJSON_Print(cjson_data)));
            cJSON_AddItemToArray(cjson_array,cjson_data);
            cJSON_AddItemToObject(cjson_token,"data",cjson_array);
            char *buf_token=NULL;
            buf_token= cJSON_Print(cjson_token);
            if(sendto(server_sockfd, buf_token, strlen(buf_token), 0 ,(struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
                perror("sendto error(communication_to_user(code6))\n");
            }
            cout<<buf_token<<endl;
        }else if(code_one==7){
            cout<<"第7个code"<<endl;
            cout<<buf<<endl;

            cJSON* n_cjson= cJSON_GetObjectItem(mjson,"station");
            which_station=n_cjson->valueint;

            cJSON* t_cjson=NULL;
            t_cjson= cJSON_GetObjectItem(mjson,"token");
            //验证token
            if (Token::findToken(t_cjson->valuestring) != t_cjson->valuestring){
                continue;
            }

            //3.4
            cJSON* cjson_token= NULL;
            cjson_token= cJSON_CreateObject();
            cJSON_AddNumberToObject(cjson_token, "code", 8);
            cJSON_AddNumberToObject(cjson_token, "station", n_cjson->valueint);
            cJSON *cjson_array=cJSON_CreateArray();

            //从文件里取保存了气象数据的日期
            std::vector<std::string> fileNames;
            std::string folderPath;
            if(n_cjson->valueint==1){
                folderPath = "/home/easy/data/store/station1";
            }else if(n_cjson->valueint==2){
                folderPath = "/home/easy/data/store/station2";
            }
            if(n_cjson->valueint==1||n_cjson->valueint==2){
                for (const auto &entry: fs::directory_iterator(folderPath)) {
                    if (entry.is_regular_file()) {
                        //std::cout << entry.path().filename().string() << std::endl;
                        fileNames.push_back(entry.path().filename().string());
                    }
                }
                std::sort(fileNames.begin(), fileNames.end());

                for (const auto& fileName : fileNames) {
                    //std::cout << fileName << std::endl;
                    cJSON_AddItemToArray(cjson_array, cJSON_CreateString(fileName.c_str()));
                }

                cJSON_AddItemToObject(cjson_token, "date", cjson_array);
                char *buf_token = NULL;
                buf_token = cJSON_Print(cjson_token);
                if (sendto(server_sockfd, buf_token, strlen(buf_token), 0, (struct sockaddr *) &client_addr,
                           sizeof(struct sockaddr)) < 0) {
                    perror("sendto error(communication_to_user(code8))\n");
                }
                cout << buf_token << endl;
            }else{
                //······
            }
        }
        else if(code_one==9){
            cout<<"第9个code"<<endl;
            cout<<buf<<endl;
            //可能检查token
            if(success==0){
                char* buf=NULL;
                buf="用户名密码错误";
                cout<<buf<<endl;
                if(sendto(server_sockfd, buf, strlen(buf), 0 ,(struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
                    perror("sendto error(communication_to_user(code10))\n");
                }
            }else{
                //识别气象站
                cJSON* station_json=cJSON_GetObjectItem(mjson,"station");
                which_station=station_json->valueint;

                //识别要求日期
                cJSON *cjson_start=NULL;
                cJSON *cjson_end;
                cjson_start= cJSON_GetObjectItem(mjson,"start");
                cjson_end= cJSON_GetObjectItem(mjson,"end");
                cout<<cjson_start->valuestring<<endl;
                cout<<cjson_end->valuestring<<endl;
                int nums_start[3]={0};
                int nums_end[3]={0};
                char *c_start=NULL;
                char *c_end=NULL;
                c_start=cjson_start->valuestring;
                c_end=cjson_end->valuestring;

                discern(nums_start,cjson_start->valuestring);
                discern(nums_end,cjson_end->valuestring);
                Date d1(nums_start[0],nums_start[1],nums_start[2]);
                Date d2(nums_end[0],nums_end[1],nums_end[2]);
                cout<<"开始日期："<<d1.toString()<<endl;
                cout<<"结束日期："<<d2.toString()<<endl;

                if(d1>(d2+1))
                    continue;

                cout<<"日期有效"<<endl;

                //string str_filename;
                vector<vector<string>> v1_cjson;
                vector<string> v2_cjson(9);
                string p="/home/easy/data/store/station"+ to_string(which_station)+"/";
                while(1){
                    v1_cjson.clear();
                    v2_cjson.clear();

                    int i=0;
                    int last=0;
                    string line;
                    cout<<"正在打开"<<p+d1.toString()<<endl;
                    ifstream file(p+d1.toString()); // 打开文件

                    if (file.is_open()) // 检查文件是否成功打开
                    {
                        while (getline(file, line)) // 逐行读取文件内容
                        {
                            //cout << line << endl; // 输出每行内容
                            if(line.length()!=1){
                                if (i < 9) {
                                    if(i==0){
                                        line= findSubstring(line,":\t\"","\",");
                                    }else if(i==8){
                                        line=getStringAfterString(line,":\t");
                                    }else{
                                        line= findSubstring(line,":\t",",");
                                    }
                                    //cJSON_AddItemToArray(cjson_array2, cJSON_CreateString(line.c_str()));
                                    v2_cjson.push_back(line);
                                    i++;
                                    if (i == 9) {
                                        //cJSON_AddItemToArray(cjson_array1, cjson_array2);
                                        v1_cjson.push_back(v2_cjson);
                                        i = 0;
                                        //cjson_array2 = cJSON_CreateArray();
                                        v2_cjson.clear();
                                    }
                                }
                            }
                        }
                        file.close(); // 关闭文件
                    }
                    else
                    {
                        cerr << "无法打开文件:" <<::strerror(errno) <<endl; // 如果文件打开失败，输出错误信息
                        //return;
                    }
                    cout<<"over\n";
                    //test what in the vector
//                    for(int cnt=0;cnt<v1_cjson.size();cnt++){
//                        for(int k=0;k<9;k++){
//                            cout<<"["<<cnt<<","<<k<<"]:"<<v1_cjson[cnt][k]<<endl;
//                        }
//                    }


                    int cnt,k,m;
                    for(cnt=0;cnt<24;cnt++){
                        //开始封装
                        cJSON *back = cJSON_CreateObject();
                        cJSON *cjson_array1 = cJSON_CreateArray();
                        cJSON *cjson_array2 = cJSON_CreateArray();


                        for(m=cnt*60;m<(cnt+1)*60;m++){
                            if(m==v1_cjson.size())
                                break;
                            for(k=0;k<9;k++){
                                cJSON_AddItemToArray(cjson_array2, cJSON_CreateString(v1_cjson[m][k].c_str()));
                            }
                            cJSON_AddItemToArray(cjson_array1,cjson_array2);
                            // noticed
                            cjson_array2=cJSON_CreateArray();
                        }

                        // noticed
                        if((strcmp(d1.toString().c_str(),d2.toString().c_str())==0)&&(m==v1_cjson.size())){
                            last=1;
                        }

                        int len_cjson_array1=cJSON_GetArraySize(cjson_array1);
                        if(!(len_cjson_array1>0))
                            success=0;

                        cJSON_AddNumberToObject(back, "code", 10);
                        cJSON_AddNumberToObject(back, "success", success);
                        cJSON_AddNumberToObject(back, "station", which_station);
                        cJSON_AddStringToObject(back, "date", d1.toString().c_str());
                        cJSON_AddNumberToObject(back, "last", last);
                        cJSON_AddItemToObject(back, "data", cjson_array1);
                        cout << cJSON_Print(back) << endl;

                        success=success_;

                        char *buf_cjson = cJSON_Print(back);
                        cJSON_Delete(back);
                        if (sendto(server_sockfd, buf_cjson, strlen(buf_cjson), 0, (struct sockaddr *) &client_addr,
                                   sizeof(struct sockaddr)) < 0) {
                            perror("sendto error(communication_to_user(code10))\n");
                            cout << "没发" << endl;
                        }
//                        cout << buf_cjson << endl;
                        if(m==v1_cjson.size()){
                            break;
                        }
                    }


                    d1+=1;

//                    // noticed
//                    if((strcmp(d1.toString().c_str(),d2.toString().c_str())==0)&&(m==v1_cjson.size())){
//                        last=1;
//                    }

                    if(last==1)
                        break;
                }

            }
        }
    }
    close(server_sockfd);
}

void communicate_to_edge(){
    char *buf_cjson=NULL;
    int server_sockfd = 0;
    int len = 0;
    socklen_t sin_size = 0;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    char buf[BUFFSIZE] = {0};

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(9090);

    if((server_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!\n");
        return ;
    }

    if(bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        perror("bind error!\n");
        return ;
    }
//    sin_size= sizeof(struct sockaddr_in);
    while(1)
    {
        sin_size= sizeof(struct sockaddr_in);
        int is=0;  //记录用户名和密码验证结果的标识符
        if((len = recvfrom(server_sockfd, buf, BUFFSIZE, 0, (struct sockaddr*)&client_addr, &sin_size)) < 0)
        {
            perror("recvfrom error!\n");
            return ;
        }

        printf("recv from: %s\n",inet_ntoa(client_addr.sin_addr));
        buf[len] = '\0';
        printf("recv is: %s\n",buf);
        if(len <= 0)
        {
            break;
        }
        //解析buf
        cJSON *cjson_buf= cJSON_Parse(buf);
        cJSON *cjson_code=NULL;
        cjson_code= cJSON_GetObjectItem(cjson_buf,"code");
        switch (cjson_code->valueint) {
            case 1:{
                int success=1;
                cJSON *cjson_name= cJSON_GetObjectItem(cjson_buf,"name");
                cJSON *cjson_password= cJSON_GetObjectItem(cjson_buf,"password");
                if(std::strcmp(cjson_name->valuestring,"root")) {
                    success = 0;
                }
                if(std::strcmp(cjson_password->valuestring,"123456")) {
                    success = 0;
                }
                is=success;
                cJSON *cjson_respone=cJSON_CreateObject();
                cJSON_AddNumberToObject(cjson_respone,"code",2);
                cJSON_AddNumberToObject(cjson_respone,"success",success);
                //弄个令牌
                Token token_to_edge(16);

                cJSON_AddStringToObject(cjson_respone,"token",token_to_edge.getToken().c_str());
                buf_cjson= cJSON_Print(cjson_respone);
                cout<<inet_ntoa(client_addr.sin_addr)<<endl;
                cout<<buf_cjson<<endl;
                if(sendto(server_sockfd,buf_cjson, std::strlen(buf_cjson),0,(struct sockaddr*)&client_addr, sizeof(struct sockaddr))<0){
                    perror("sendto(communicate to edge code2)");
                }
                cJSON_Delete(cjson_respone);
            }
                break;
            case 3:  {
                    cJSON *cjson_token= cJSON_GetObjectItem(cjson_buf,"token");
                    cJSON *cjson_respone=cJSON_CreateObject();
                    cJSON_AddNumberToObject(cjson_respone,"code",4);
                    cJSON_AddStringToObject(cjson_respone,"token",cjson_token->valuestring);
                    //做一些事件废除令牌
                    int success = 1;
                    if(Token::removeToken(cjson_token->valuestring)) {
                        success = 1;
                    }else{
                        success = 0;
                        std::cout<<"WARNING：非法令牌"<<endl;
                    }
                    cJSON_AddNumberToObject(cjson_respone,"success",success);
                    buf_cjson= cJSON_Print(cjson_respone);
                    if(sendto(server_sockfd,buf_cjson, std::strlen(buf_cjson),0,(struct sockaddr*)&client_addr, sizeof(struct sockaddr))<0){
                        perror("sendto(communicate to edge code4)");
                    }
                    cJSON_Delete(cjson_respone);
            }
                break;
            case 5:{
                deal_data(buf);
                char *addr=NULL;
                addr=inet_ntoa(client_addr.sin_addr);
                deal_user(buf,addr);
                cJSON *cjson_respone=cJSON_CreateObject();
                cJSON_AddNumberToObject(cjson_respone,"code",6);
                int success = 1;
                cJSON_AddNumberToObject(cjson_respone,"success",1);
                buf_cjson= cJSON_Print(cjson_respone);
                std::cout<<"***********************"<<std::endl;
                std::cout<<buf_cjson<<std::endl;
                if(sendto(server_sockfd,buf_cjson, std::strlen(buf_cjson),0,(struct sockaddr*)&client_addr, sizeof(struct sockaddr))<0){
                    perror("sendto(communicate to edge code6)");
                }
                cJSON_Delete(cjson_respone);
            }
                break;
        }
        if(cjson_code->valueint==3)
            break;
    }
    close(server_sockfd);
}

